/**
 * convertCurrencyToChinese - 数字转成汉字
 * @params num === 要转换的数字
 * @return 汉字
 * 
*/
const Index = (num) => {
  if(!num){
    return '零';
  }
  // Constants:
  const MAXIMUM_NUMBER = 99999999999.99;
  // Predefine the radix characters and currency symbols for output:
  const CN_ZERO = "零";
  const CN_ONE = "壹";
  const CN_TWO = "贰";
  const CN_THREE = "叁";
  const CN_FOUR = "肆";
  const CN_FIVE = "伍";
  const CN_SIX = "陆";
  const CN_SEVEN = "柒";
  const CN_EIGHT = "捌";
  const CN_NINE = "玖";
  const CN_TEN = "拾";
  const CN_HUNDRED = "佰";
  const CN_THOUSAND = "仟";
  const CN_TEN_THOUSAND = "万";
  const CN_HUNDRED_MILLION = "亿";
  // const CN_SYMBOL = "人民币";
  const CN_DOLLAR = "元";
  const CN_TEN_CENT = "角";
  const CN_CENT = "分";
  const CN_INTEGER = "整";
  // Variables:
  // let integral; // Represent integral part of digit number.
  // let decimal; // Represent decimal part of digit number.
  let outputCharacters; // The output result.
  // let parts;
  // let digits;
  // let radices;
  // let bigRadices;
  // let decimals;
  let zeroCount;
  let i;
  let p;
  let d;
  let quotient;
  let modulus;
  let currencyDigits = num;
  // Validate input string:
  currencyDigits = currencyDigits.toString();
  if (currencyDigits === "") {
    // alert("Empty input!");
    return "";
  }
  if (currencyDigits.match(/[^,.\d]/) != null) {
    // alert("Invalid characters in the input string!");
    return "";
  }
  if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) {
    // alert("Illegal format of digit number!");
    return "";
  }
  // Normalize the format of input digits:
  currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters.
  currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning.
  // Assert the number is not greater than the maximum number.
  if (Number(currencyDigits) > MAXIMUM_NUMBER) {
    // eslint-disable-next-line no-console
    console.warn("输入的金额太大，请重新输入!");
    return "";
  }
  // Process the coversion from currency digits to characters:
  // Separate integral and decimal parts before processing coversion:
  const parts = currencyDigits.split(".");
  // eslint-disable-next-line prefer-const
  let [integral, decimal = ''] = parts;
  if (parts.length > 1) {
    // Cut down redundant decimal digits that are after the second.
    decimal = decimal.substr(0, 2);
  }
  // Prepare the characters corresponding to the digits:
  const digits = [CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE];
  const radices = ["", CN_TEN, CN_HUNDRED, CN_THOUSAND];
  const bigRadices = ["", CN_TEN_THOUSAND, CN_HUNDRED_MILLION];
  const decimals = [CN_TEN_CENT, CN_CENT];
  // Start processing:
  outputCharacters = "";
  // Process integral part if it is larger than 0:
  if (Number(integral) > 0) {
    zeroCount = 0;
    for (i = 0; i < integral.length; i++) {
      p = integral.length - i - 1;
      d = integral.substr(i, 1);
      quotient = p / 4;
      modulus = p % 4;
      if (d === "0") {
        zeroCount++;
      }
      else {
        if (zeroCount > 0) {
          outputCharacters += digits[0];
        }
        zeroCount = 0;
        outputCharacters += digits[Number(d)] + radices[modulus];
      }
      if (modulus === 0 && zeroCount < 4) {
        outputCharacters += bigRadices[quotient];
      }
    }
    outputCharacters += CN_DOLLAR;
  }
  // Process decimal part if there is:
  if (decimal !== "") {
    for (i = 0; i < decimal.length; i++) {
      d = decimal.substr(i, 1);
      if (d !== "0") {
        outputCharacters += digits[Number(d)] + decimals[i];
      }
    }
  }
  // Confirm and return the final output string:
  if (outputCharacters === "") {
    outputCharacters = CN_ZERO + CN_DOLLAR;
  }
  if (decimal === "") {
    outputCharacters += CN_INTEGER;
  }
  return outputCharacters || '';
}
export default Index